package cn.wolfcode.mq.listener;

import cn.wolfcode.common.web.Result;
import cn.wolfcode.mq.MQConstant;
import cn.wolfcode.mq.OrderMQResult;
import cn.wolfcode.mq.OrderMessage;
import cn.wolfcode.service.IOrderInfoService;
import cn.wolfcode.web.msg.SeckillCodeMsg;
import cn.wolfcode.web.sendCallback.DefaultSendCallback;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

@Slf4j
@RocketMQMessageListener(
        topic = MQConstant.ORDER_PENDING_TOPIC,
        selectorExpression = MQConstant.ORDER_PENDING_TAG,
        consumerGroup = MQConstant.ORDER_PENDING_GROUP
)

@Component
public class PendingOrderMessageListener implements RocketMQListener<OrderMessage> {

    @Autowired
    private IOrderInfoService orderInfoService;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    // @Override
    // public void onMessage(OrderMessage msg) {
    //     log.info("订单消息测试: topic={}, tag={}, msgId={}");
    // }

    @Override
    public void onMessage(OrderMessage msg) {

        log.info("[创建订单监听器] 收到新的订单, 请查收 msg={}", JSON.toJSONString(msg));
        OrderMQResult result = new OrderMQResult(msg.getTime(), msg.getSeckillId(),
                msg.getToken(), null, "订单创建成功", Result.SUCCESS_CODE);
        try {
            String orderNo = orderInfoService.createOrder(msg.getUserPhone(), msg.getSeckillId());
            // 创建订单成功
            log.info("[创建订单监听器] 订单创建成功, 订单编号:{}，　即将通知订单创建结果..., 请查收", orderNo);

            // 创建订单结果对象
            result.setOrderNo(orderNo);

            // 处理超时订单问题
            // 1. 利用 java 调度线程池执行一条延迟任务
            // 2. 利用定时任务每分钟查询超过 x分钟 未支付的订单
            // 3. 利用 rocketMQ 的延迟消息, 直接发布延迟消息, 等到时间后进行消费. 检查订单是否已支付

            // 构建消息对象
            Message<OrderMQResult> message = MessageBuilder.withPayload(result).build();
            rocketMQTemplate.asyncSend(MQConstant.ORDER_PAY_TIMEOUT_TOPIC, message,
                    new DefaultSendCallback("延迟消息", orderNo), 2000, 9);
        } catch (Exception e) {
            log.error("[创建订单监听器] 订单下单失败...");
            // 订单创建失败
            result.setMsg(SeckillCodeMsg.SECKILL_ERROR.getMsg());
            result.setCode(SeckillCodeMsg.SECKILL_ERROR.getCode());
        } finally {
            // 发送消息
             rocketMQTemplate.asyncSend(MQConstant.ORDER_RESULT_SUCCESS_DEST, result, new DefaultSendCallback("订单创建结果", result));
        }
    }
}
